function [thickness] = TrabecularThicknessMeasurement(img_bone, image_width, image_height, pixel_spacing)
% 测量骨小梁厚度

% 输入参数：
% img_bone            :  骨的超声或光声图像, 单通道
% image_width     : 图像宽度
% image_height    : 图像高度

% 输出参数：
%thickness   ： 骨小梁平均厚度


img_bone = reshape(img_bone, image_height, image_width);

dx = pixel_spacing;% mm 单象素物理尺寸
img_bone= im2bw(img_bone,0.35);%Step 1: 图像二值化 
%%img_bone = 1-double(img_bone);%取反

S = bwmorph(img_bone,'skeleton',Inf);   % Step2: 提取骨架
S = bwmorph(S,'spur',4);     % 去毛刺

% bone2=imbinarize(img_bone); % 也可用于提取骨架
% S=bwskel(bone2)

thickness = sum(sum(img_bone))/sum(sum(S))*dx %  






figure('Name','骨小梁二值化特征提取','NumberTitle','off','toolbar','figure','menubar','none'),  % 不显示工具栏，菜单栏
imagesc(img_bone);
colormap bone
title(['Bone binarization processing'],'fontsize',16,'fontname','Times','FontWeight','bold')

ylabel('[mm]')
xlabel('[mm]')
set(gca, 'box', 'off',...
         'linewidth',4.5,...
         'TickDir','in',...
         'fontsize',16,'fontname','Times','FontWeight','bold','dataaspectratio',[1,1,1],'XTick', [1,round(image_width/2,2),round(image_width,2)],'XTickLabel', [0,round(image_width/2*dx,2),round(image_width*dx,2)],'YTick', [1,round(image_height/2,2),round(image_height,2)],'YTickLabel', [0,round(image_height/2*dx,2),round(image_height*dx,2)],'box','on')




figure('Name','骨小梁空间分布','NumberTitle','off','toolbar','figure','menubar','none'),  % 不显示工具栏，菜单栏
imagesc(S);
colormap copper
title(['Bone Skeleton Estimated Thickness = ',num2str(round(thickness,3)), ' mm'],'fontsize',16,'fontname','Times','FontWeight','bold')

ylabel('[mm]')
xlabel('[mm]')
set(gca, 'box', 'off',...
         'linewidth',4.5,...
         'TickDir','in',...
         'fontsize',16,'fontname','Times','FontWeight','bold','dataaspectratio',[1,1,1],'XTick', [1,round(image_width/2,2),round(image_width,2)],'XTickLabel', [0,round(image_width/2*dx,2),round(image_width*dx,2)],'YTick', [1,round(image_height/2,2),round(image_height,2)],'YTickLabel', [0,round(image_height/2*dx,2),round(image_height*dx,2)],'box','on')











end